<template>
  <view class="ver-layout">
    <view class="top-tabs hor-layout">
      <view v-for="(item,index) in tabList" class="tabs-view" :class="{'select':item.value===selectTabValue}"
            @click="tabClick(item)">
        {{ item.name }}
      </view>
    </view>
    <!--  作业列表  -->
    <view class="homework-list">
      <view v-if="homeworkList.length<=0" class="list-no-data">暂无数据</view>
      <view v-else class="homework-item hor-layout" :class="{'top':index===0}" v-for="(item,index) in homeworkList"
            :key="index" @click="toHomeWorkDetail(item)">
        <view class="homework-tag" :class="getItemTag(item).class">{{ getItemTag(item).text }}</view>
        <view class="homework-content hor-layout-center">
          <image class="homework-icon" :src="item.material_image"/>
          <view class="homework-content-texts ver-layout">
            <view class="content-title">{{ item.material_title }}</view>
            <view class="content-time">作业布置时间：{{ item.homework_time }}</view>
          </view>
          <view class="homework-detail">查看</view>
        </view>
      </view>
      <u-loadmore margin-top="15" v-if="loadingObj.status" :status="loadingObj.status"/>
    </view>
  </view>
</template>

<script>
import {recordStudentHomeworkLayout, recordStudentHomeworkPush} from "@/router.js"
import {StudentHomeWorkApi} from "@/api/recordStudy.js"

let engine = new StudentHomeWorkApi()
export default {
  name: "homework",
  data() {
    return {
      studentObj: {},
      tabList: [{name: '已完成', value: 20}, {name: '待完成', value: 10}],
      selectTabValue: 20,
      homeworkList: [{}],
      loadingObj: {
        status: '',
        current: 1,
        total: ''
      },
    }
  },
  onLoad(options) {
    this.studentObj = options
    // this.refreshData()
  },
  onReachBottom(e) {
    this.loadMore()
  },
  methods: {
    getHomeWorkList() {
      let type = this.selectTabValue
      let sid = this.studentObj.student_id,
          page = this.loadingObj.current
      engine.getHomeWorkList({page, sid, type}).then(res => {
        this.homeworkList = this.homeworkList.concat(res.data)
        this.loadFinish(res.total)
      })
    },
    tabClick(item) {
      this.selectTabValue = item.value
      this.refreshData()
    },
    getItemTag(item) {
      if (!item.complete_status||item.complete_status === 10) {
        return {class: "uncompleted", text: "未完成"}
      } else if (item.status === 0) {
        return {class: '', text: '未审核'}
      } else if (item.status === 1) {
        return {class: "checked", text: '已审核'}
      }
    },
    toHomeWorkDetail(item) {//完成状态【10-未完成，20-已完成】
      let url = recordStudentHomeworkLayout
      if (item.complete_status === 20) url = recordStudentHomeworkPush
      this.$u.route({url, params: item})
    },
    refreshData() {
      this.loadingObj.current = 1
      this.loadingObj.status = ''
      this.homeworkList = []
      this.getHomeWorkList()
    },
    loadMore() {
      if (this.loadingObj.current >= this.loadingObj.total) {
        this.loadingObj.status = 'nomore'
      } else {
        this.loadingObj.status = 'loading'
        this.loadingObj.current = this.loadingObj.current + 1
        this.getHomeWorkList()
      }
    },
    loadFinish(total) {
      this.loadingObj.total = Math.ceil(total / 10)
      this.loadingObj.status = ''
      if (this.loadingObj.current >= this.loadingObj.total
          && this.homeworkList.length > 0) {
        this.loadingObj.status = 'nomore'
      }
    },
  }
}
</script>

<style>
page {
  background: #F2F2F2;
}

.top-tabs {
  position: fixed;
  width: 100%;
  height: 37px;
  background: #FFFFFF;
}

.tabs-view {
  flex: 1;
  line-height: 37px;
  text-align: center;
  /* 文本样式 */
  font-size: 12px;
  font-family: PingFang SC;
  font-weight: 400;
}

.tabs-view.select {
  color: #42B4AB;
  border-bottom: 1px solid #42B4AB;
}

.homework-list {
  margin-top: 37px;
}

.homework-item {
  margin: auto;
  margin-bottom: 18px;
  width: calc(100% - 24px);
  height: 54px;
  background: #FFFFFF;
  border-radius: 6px;
}

.homework-item.top {
  margin-top: 18px;
}

.homework-tag {
  margin-left: 13px;
  width: 20px;
  height: fit-content;
  padding: 3px 0px;
  background: #FF873E;
  border-radius: 0px 0px 10px 10px;
  text-align: center;
  line-height: 13px;
  /* 文本样式 */
  font-size: 10px;
  color: #FFFFFF;
}

.homework-tag.checked {
  background: #42B4AB;
}

.homework-tag.uncompleted {
  background: #F64224;
}

.homework-icon {
  width: 30px;
  height: 30px;
  border-radius: 50%;
}

.homework-content {
  width: calc(100% - 26px);
  margin: 0px 13px;
}

.homework-content-texts {
  flex: 1;
  margin-left: 11px;
}

.content-title {
  font-size: 14px;
  line-height: 14px;
  color: #333333;
}

.content-time {
  margin-top: 7px;
  font-size: 10px;
  line-height: 10px;
  color: #333333;
}

.homework-detail {
  font-size: 13px;
  color: #FF873E;
}
</style>
